﻿using System;
using System.Collections.Generic;
using SurveySystem.Model;
using System.Data.SqlClient;
using SurveySystem.Utility;

namespace SurveySystem.Repository.MySQL
{
    public class MssqlAnswerRepository : ISurveyAnswerRepository
    {
        public Answer GetAnswerByID(int answerID)
        {
            string sql;
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            Answer a = null;

            sql = "SELECT `answer_id`, `question_id`, `answer_text` \n" + //
                  "FROM `survey_answer` WHERE answer_id=@id";
            try
            {
                conn = new SqlConnection(Globals.ConnectionString);
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", answerID);

                conn.Open();
                dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    a = new Answer();

                    a.AnswerID = dr.GetInt32(0);
                    a.QuestionID = dr.GetInt32(1);
                    a.AnswerText = dr.GetString(2);

                }
                else
                {
                    a = null;
                }

                conn.Close();
            }
            catch (SqlException ex)
            {
                // TODO: logging
                throw ex;
            }

            return a;
        }

        public List<Answer> GetAnswersForQuestion(int questionID)
        {
            string sql;
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            List<Answer> list = new List<Answer>();

            sql = "SELECT `answer_id`, `question_id`, `answer_text` \n" + //
                  "FROM `survey_answer` WHERE question_id=@id";
            try
            {
                conn = new SqlConnection(Globals.ConnectionString);
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", questionID);

                conn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    Answer a = new Answer();

                    a.AnswerID = dr.GetInt32(0);
                    a.QuestionID = dr.GetInt32(1);
                    a.AnswerText = dr.GetString(2);

                    list.Add(a);
                }


                conn.Close();
            }
            catch (SqlException ex)
            {
                // TODO: logging
                throw ex;
            }

            return list;
        }

        public List<Answer> GetAnswersForSurvey(int surveyID)
        {
            string sql;
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            List<Answer> list = new List<Answer>();

            sql = "SELECT `answer_id`, `question_id`, `answer_text` \n" + //
                  "FROM `survey_answer` WHERE survey_id=@id";
            try
            {
                conn = new SqlConnection(Globals.ConnectionString);
                cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", surveyID);

                conn.Open();
                dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    Answer a = new Answer();

                    a.AnswerID = dr.GetInt32(0);
                    a.QuestionID = dr.GetInt32(1);
                    a.AnswerText = dr.GetString(2);

                    list.Add(a);
                }


                conn.Close();
            }
            catch (SqlException ex)
            {
                // TODO: logging
                throw ex;
            }

            return list;
        }

        public void AddAnswer(Answer a)
        {
            throw new NotImplementedException();
        }

        public void UpdateAnswer(Answer a)
        {
            throw new NotImplementedException();
        }

        public void DeleteAnswer(int answerID)
        {
            throw new NotImplementedException();
        }
    }
}
